#include "wxssimplefonteditordlg.h"
#include "wxsfonteditordlg.h"

#include <wx/fontdlg.h>

#include <wx/intl.h>
#include <wx/string.h>

const long wxsSimpleFontEditorDlg::ID_STATICTEXT1 = wxNewId();
const long wxsSimpleFontEditorDlg::ID_STATICLINE2 = wxNewId();
const long wxsSimpleFontEditorDlg::ID_BUTTON1 = wxNewId();
const long wxsSimpleFontEditorDlg::ID_BUTTON3 = wxNewId();
const long wxsSimpleFontEditorDlg::ID_STATICLINE1 = wxNewId();
const long wxsSimpleFontEditorDlg::ID_BUTTON2 = wxNewId();
const long wxsSimpleFontEditorDlg::ID_TEXTCTRL1 = wxNewId();
const long wxsSimpleFontEditorDlg::ID_STATICTEXT2 = wxNewId();
const long wxsSimpleFontEditorDlg::ID_STATICTEXT3 = wxNewId();

BEGIN_EVENT_TABLE( wxsSimpleFontEditorDlg, wxScrollingDialog )


  EVT_BUTTON( wxID_OK, wxsSimpleFontEditorDlg::OnOK )
END_EVENT_TABLE()

wxsSimpleFontEditorDlg::wxsSimpleFontEditorDlg( wxWindow* parent, wxsFontData& Data, wxWindowID id ):
  m_Data( Data ) {
  wxBoxSizer* BoxSizer4;
  wxStaticBoxSizer* StaticBoxSizer2;
  wxBoxSizer* BoxSizer1;
  wxFlexGridSizer* FlexGridSizer1;
  wxBoxSizer* BoxSizer3;
  wxStdDialogButtonSizer* StdDialogButtonSizer1;
  Create( parent, id, _T( "Font settings" ), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _T( "id" ) );
  FlexGridSizer1 = new wxFlexGridSizer( 0, 1, 0, 0 );
  BoxSizer4 = new wxBoxSizer( wxHORIZONTAL );
  StaticBoxSizer1 = new wxStaticBoxSizer( wxHORIZONTAL, this, _T( "Current font" ) );
  FlexGridSizer2 = new wxFlexGridSizer( 0, 0, 0, 0 );
  FlexGridSizer2->AddGrowableCol( 0 );
  FlexGridSizer2->AddGrowableRow( 0 );
  FontDescription = new wxStaticText( this, ID_STATICTEXT1, _T( "-- None --" ), wxDefaultPosition, wxDefaultSize, 0, _T( "ID_STATICTEXT1" ) );
  FlexGridSizer2->Add( FontDescription, 0, wxALL | wxALIGN_CENTER_VERTICAL, 5 );
  StaticBoxSizer1->Add( FlexGridSizer2, 1, wxEXPAND, 5 );
  StaticLine2 = new wxStaticLine( this, ID_STATICLINE2, wxDefaultPosition, wxDefaultSize, wxLI_VERTICAL, _T( "ID_STATICLINE2" ) );
  StaticBoxSizer1->Add( StaticLine2, 0, wxLEFT | wxEXPAND, 5 );
  BoxSizer2 = new wxBoxSizer( wxVERTICAL );
  Button1 = new wxButton( this, ID_BUTTON1, _T( "Change" ), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T( "ID_BUTTON1" ) );
  BoxSizer2->Add( Button1, 0, wxALIGN_CENTER_HORIZONTAL, 5 );
  Button3 = new wxButton( this, ID_BUTTON3, _T( "Clear" ), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T( "ID_BUTTON3" ) );
  BoxSizer2->Add( Button3, 1, wxTOP | wxALIGN_CENTER_HORIZONTAL, 5 );
  StaticLine1 = new wxStaticLine( this, ID_STATICLINE1, wxDefaultPosition, wxSize( 10, -1 ), wxLI_HORIZONTAL, _T( "ID_STATICLINE1" ) );
  BoxSizer2->Add( StaticLine1, 0, wxTOP | wxEXPAND, 5 );
  Button2 = new wxButton( this, ID_BUTTON2, _T( "Advanced" ), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T( "ID_BUTTON2" ) );
  BoxSizer2->Add( Button2, 0, wxTOP | wxALIGN_CENTER_HORIZONTAL, 5 );
  StaticBoxSizer1->Add( BoxSizer2, 0, wxLEFT | wxEXPAND, 5 );
  BoxSizer4->Add( StaticBoxSizer1, 1, wxEXPAND, 5 );
  FlexGridSizer1->Add( BoxSizer4, 1, wxEXPAND, 4 );
  StaticBoxSizer2 = new wxStaticBoxSizer( wxHORIZONTAL, this, _T( "Test area" ) );
  TestArea = new wxTextCtrl( this, ID_TEXTCTRL1, _T( "This is sample text" ), wxDefaultPosition, wxSize( 275, 71 ), wxTE_MULTILINE, wxDefaultValidator, _T( "ID_TEXTCTRL1" ) );
  StaticBoxSizer2->Add( TestArea, 1, wxEXPAND, 4 );
  FlexGridSizer1->Add( StaticBoxSizer2, 1, wxEXPAND, 5 );
  BoxSizer3 = new wxBoxSizer( wxHORIZONTAL );
  StaticText2 = new wxStaticText( this, ID_STATICTEXT2, _T( "Note:" ), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE, _T( "ID_STATICTEXT2" ) );
  BoxSizer3->Add( StaticText2, 0, wxEXPAND, 5 );
  StaticText3 = new wxStaticText( this, ID_STATICTEXT3, _T( "In order to provide best cross-platform compatibility\nyou should use either system-based font or multiple \nface names. You can change it in advanced options." ), wxDefaultPosition, wxDefaultSize, 0, _T( "ID_STATICTEXT3" ) );
  BoxSizer3->Add( StaticText3, 1, wxLEFT | wxEXPAND, 5 );
  FlexGridSizer1->Add( BoxSizer3, 1, wxTOP | wxALIGN_CENTER_VERTICAL, 5 );
  BoxSizer1 = new wxBoxSizer( wxHORIZONTAL );
  StdDialogButtonSizer1 = new wxStdDialogButtonSizer();
  StdDialogButtonSizer1->AddButton( new wxButton( this, wxID_OK, wxEmptyString ) );
  StdDialogButtonSizer1->AddButton( new wxButton( this, wxID_CANCEL, wxEmptyString ) );
  StdDialogButtonSizer1->Realize();
  BoxSizer1->Add( StdDialogButtonSizer1, 0, wxEXPAND, 5 );
  FlexGridSizer1->Add( BoxSizer1, 0, wxTOP | wxALIGN_CENTER_VERTICAL, 5 );
  SetSizer( FlexGridSizer1 );
  FlexGridSizer1->Fit( this );
  FlexGridSizer1->SetSizeHints( this );
  Center();
  Connect( ID_BUTTON1, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( wxsSimpleFontEditorDlg::OnButton1Click ) );
  Connect( ID_BUTTON3, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( wxsSimpleFontEditorDlg::OnButton3Click ) );
  Connect( ID_BUTTON2, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( wxsSimpleFontEditorDlg::OnButton2Click ) );
  m_WorkingCopy = m_Data;
  UpdateFontDescription();
}

wxsSimpleFontEditorDlg::~wxsSimpleFontEditorDlg() {
}

void wxsSimpleFontEditorDlg::UpdateFontDescription() {
  wxString Description = _T( "-- None --" );
  if( !m_WorkingCopy.IsDefault ) {
    Description.Clear();
    if( m_WorkingCopy.HasSysFont ) {
      Description << wxString::Format( _T( "System-based (%s)\n" ), m_WorkingCopy.SysFont.c_str() );
    } else if( m_WorkingCopy.Faces.Count() > 0 ) {
      Description << wxString::Format( _T( "Face: %s%s\n" ), m_WorkingCopy.Faces[0].c_str(), ( m_WorkingCopy.Faces.Count() > 1 ) ? _T( ", ..." ) : _T( "" ) );
    }
    if( m_WorkingCopy.HasSize ) {
      Description << wxString::Format( _T( "Size: %ld\n" ), m_WorkingCopy.Size );
    } else if( m_WorkingCopy.HasRelativeSize && m_WorkingCopy.HasSysFont ) {
      Description << wxString::Format( _T( "Relative size: %.2f" ), m_WorkingCopy.RelativeSize );
    }
    if( m_WorkingCopy.HasWeight ) {
      Description << _T( "Weight: " );
      switch( m_WorkingCopy.Weight ) {
        case wxFONTWEIGHT_BOLD:
          Description << _T( "Bold\n" );
          break;
        case wxFONTWEIGHT_LIGHT:
          Description << _T( "Light\n" );
          break;
        case wxFONTWEIGHT_NORMAL:
        default:
          Description << _T( "Normal\n" );
      }
    }
    if( m_WorkingCopy.HasStyle ) {
      Description << _T( "Style: " );
      switch( m_WorkingCopy.Style ) {
        case wxFONTSTYLE_ITALIC:
          Description << _T( "Italic\n" );
          break;
        case wxFONTSTYLE_SLANT:
          Description << _T( "Slant\n" );
          break;
        case wxFONTWEIGHT_NORMAL:
        default:
          Description << _T( "Normal\n" );
      }
    }
    if( m_WorkingCopy.HasUnderlined ) {
      Description << _T( "Underlined: " );
      if( m_WorkingCopy.Underlined ) {
        Description << _T( "Yes\n" );
      } else
      { Description << _T( "No\n" ); }
    }
  }
  FontDescription->SetLabel( Description );
  Layout();
  GetSizer()->SetSizeHints( this );
  TestArea->SetFont( m_WorkingCopy.BuildFont() );
}

void wxsSimpleFontEditorDlg::OnOK( M_Code_unused wxCommandEvent& event ) {
  m_Data = m_WorkingCopy;
  EndModal( wxID_OK );
}


void wxsSimpleFontEditorDlg::OnButton1Click( M_Code_unused wxCommandEvent& event ) {
  wxFont Font = ::wxGetFontFromUser( 0, m_WorkingCopy.BuildFont() );
  if( !Font.Ok() ) {
    return;
  }
  m_WorkingCopy.Size = Font.GetPointSize();
  m_WorkingCopy.Style = static_cast<wxFontStyle>( Font.GetStyle() );
  m_WorkingCopy.Weight = static_cast<wxFontWeight>( Font.GetWeight() );
  m_WorkingCopy.Family = static_cast<wxFontFamily>( Font.GetFamily() );
  m_WorkingCopy.Underlined = Font.GetUnderlined();
  m_WorkingCopy.Faces.Clear();
  m_WorkingCopy.Faces.Add( Font.GetFaceName() );
  m_WorkingCopy.IsDefault = false;
  m_WorkingCopy.HasSize = true;
  m_WorkingCopy.HasStyle = true;
  m_WorkingCopy.HasWeight = true;
  m_WorkingCopy.HasUnderlined = true;
  m_WorkingCopy.HasFamily = true;
  m_WorkingCopy.HasEncoding = false;
  m_WorkingCopy.HasSysFont = false;
  m_WorkingCopy.HasRelativeSize = false;
  UpdateFontDescription();
}

void wxsSimpleFontEditorDlg::OnButton2Click( M_Code_unused wxCommandEvent& event ) {
  wxsFontEditorDlg Dlg( this, m_WorkingCopy );
  Dlg.ShowModal();
  UpdateFontDescription();
}

void wxsSimpleFontEditorDlg::OnButton3Click( M_Code_unused wxCommandEvent& event ) {
  m_WorkingCopy.IsDefault = true;
  UpdateFontDescription();
}
